package com.badboy.service;

import com.badboy.entity.Forums;
import com.badboy.mapper.ForumsMapper;
import com.badboy.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ForumService {

    public List<Forums> getAllForums() {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            ForumsMapper mapper = session.getMapper(ForumsMapper.class);
            return mapper.getAllForums();
        }
    }

    public Forums getForumById(Integer forumId) {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            ForumsMapper mapper = session.getMapper(ForumsMapper.class);
            return mapper.getForumById(forumId);
        }
    }

    public boolean addForum(Forums forum) {
        try (SqlSession session = MyBatisUtil.getSqlSession(false)) {
            ForumsMapper mapper = session.getMapper(ForumsMapper.class);
            int result = mapper.insertForum(forum);
            session.commit();
            return result > 0;
        }
    }

    public boolean updateForum(Forums forum) {
        try (SqlSession session = MyBatisUtil.getSqlSession(false)) {
            ForumsMapper mapper = session.getMapper(ForumsMapper.class);
            int result = mapper.updateForum(forum);
            session.commit();
            return result > 0;
        }
    }

    public boolean deleteForum(Integer forumId) {
        try (SqlSession session = MyBatisUtil.getSqlSession(false)) {
            ForumsMapper mapper = session.getMapper(ForumsMapper.class);
            int result = mapper.deleteForum(forumId);
            session.commit();
            return result > 0;
        }
    }

    public List<Forums> getForumsByAdminId(Integer adminId) {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            ForumsMapper mapper = session.getMapper(ForumsMapper.class);
            return mapper.getForumsByAdminId(adminId);
        }
    }
}